<template>
  <div style="height: 100%" class="main">
    <Layout>
      <keep-alive :include="cacheList">
              <router-view/>
            </keep-alive>
    </Layout>
  </div>
</template>
<script>
import { mapMutations, mapActions, mapGetters } from "vuex";
import { getNewTagList, getNextRoute, routeEqual } from "@/libs/util";
import routers from "@/router/routers";
import minLogo from "@/assets/images/logo-min.png";
import maxLogo from "@/assets/images/logo.png";
import { getData } from "@/api/data";
import "./main.less";
export default {
  inject: ["reload"],
  name: "Main",
  components: {
  },
  data() {
    return {
      collapsed: false,
      messagenum: "",
      showMessage: false,
      mesLun: null,
      minLogo,
      maxLogo,
      isFullscreen: false,
      counter: 0
    };
  },
  computed: {
    ...mapGetters(["errorCount"]),
    tagNavList() {
      let tagNavList = this.$store.state.app.tagNavList.filter(item=>{
        return item.name!=='home'
      })
      return this.$store.state.app.tagNavList;
    },
    tagRouter() {
      return this.$store.state.app.tagRouter;
    },
    userAvator() {
      return this.$store.state.user.avatorImgPath;
    },
    cacheList() {
      return [
        "ParentView",
        ...(this.tagNavList.length
          ? this.tagNavList
              .filter(item => !(item.meta && item.meta.notCache))
              .map(item => item.name)
          : [])
      ];
    },
    menuList() {
      return this.$store.getters.menuList;
    },
    local() {
      return this.$store.state.app.local;
    },
    hasReadErrorPage() {
      return this.$store.state.app.hasReadErrorPage;
    },
    unreadCount() {
      return this.$store.state.user.unreadCount;
    },
    unreaDrawing() {
      return this.$store.state.user.unreaDrawing;
    },
    subDrawing() {
      return this.$store.state.user.subDrawing;
    }
  },
  methods: {
    ...mapMutations([
      "setBreadCrumb",
      "setTagNavList",
      "addTag",
      "setLocal",
      "setHomeRoute"
    ]),
    ...mapActions(["handleLogin", "getUnreadMessageCount"]),
    jumeshan() {
      this.$router.push({ path: "/bussiness/recommend-list" });
    },
    jumpfince() {
      this.$router.push({ path: "/finance/list", query: { type: "bus" } });
    },
    jumpsubfince() {
      this.$router.push({ path: "/finance/list", query: { type: "sub" } });
    },
    goHome() {
      this.$router.push({
        name: "home"
      });
    },
    turnToPage(route) {
      let { name, params, query } = {};
      if (typeof route === "string") name = route;
      else {
        name = route.name;
        params = route.params;
        query = route.query;
      }
      if (name.indexOf("isTurnByHref_") > -1) {
        window.open(name.split("_")[1]);
        return;
      }
      this.$router.push({
        name,
        params,
        query
      });
      if (!params) {
        setTimeout(() => {
          this.reload();
        }, 1);
      }
    },
    handleCollapsedChange(state) {
      this.collapsed = state;
    },
    handleCloseTag(res, type, route) {
      if (type === "all") {
        this.turnToPage(this.$config.homeName);
      } else if (routeEqual(this.$route, route)) {
        if (type !== "others") {
          const nextRoute = getNextRoute(this.tagNavList, route);
          this.$router.push(nextRoute);
        }
      }
      this.setTagNavList(res);
    },
    handleClick(item) {
      this.turnToPage(item);
    }
  },
  watch: {
    $route(newRoute) {
      const { name, query, params, meta } = newRoute;
      this.addTag({
        route: { name, query, params, meta },
        type: "push"
      });
      this.setBreadCrumb(newRoute);
      this.setTagNavList(getNewTagList(this.tagNavList, newRoute));
      // this.$refs.sideMenu.updateOpenName(newRoute.name);
    }
  },
  mounted() {
    /**
     * @description 初始化设置面包屑导航和标签导航
     */
    this.setTagNavList();
    this.setHomeRoute(routers);
    this.addTag({
      route: this.$store.state.app.homeRoute
    });
    this.setBreadCrumb(this.$route);
    // 设置初始语言
    this.setLocal(this.$i18n.locale);
    // 如果当前打开页面不在标签栏中，跳到homeName页
    if (!this.tagNavList.find(item => item.name === this.$route.name)) {
      this.$router.push({
        name: this.$config.homeName
      });
    }
    this.getUnreadMessageCount();
  }
};
</script>
